package com.tianyang.yukuaifu.business.trade.view;

import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseTitleActivity;
import com.tianyang.yukuaifu.business.trade.adapter.EarningsAdapter;
import com.tianyang.yukuaifu.business.trade.bean.AgentWalletOutput;
import com.tianyang.yukuaifu.business.trade.bean.EarningsOutput;
import com.tianyang.yukuaifu.business.trade.bean.EarningsTypeOutput;
import com.tianyang.yukuaifu.business.trade.contract.EarningsContract;
import com.tianyang.yukuaifu.business.trade.presenter.EarningsPresenter;
import com.tianyang.yukuaifu.business.trade.provider.TradeOpenProvider;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.module.dialog.interfaces.ICallbackListener;
import com.tianyang.yukuaifu.module.widget.VerticalSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Description：收益
 * Created by xyj on 2024/3/10
 */
public class EarningsActivity extends BaseTitleActivity implements EarningsContract.View {

    @BindView(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView vRecyclerview;

    private RelativeLayout emptyView;

    private EarningsHeaderView vHeaderView;

    private EarningsAdapter vEarningsAdapter;

    private EarningsContract.Presenter mPresenter;

    private int nowPage = CommonConfig.PAGE_NUM, serviceType;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_recycler_view;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("钱包");
        commonTitle.showRightTextView("提现记录");

        vRecyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        vEarningsAdapter = new EarningsAdapter();
        vHeaderView = new EarningsHeaderView(mContext, null);
        vHeaderView.setCallbackListener(new ICallbackListener<Integer>() {
            @Override
            public void callback(Integer value) {
                if (serviceType != value) {
                    serviceType = value;
                    nowPage = CommonConfig.PAGE_NUM;
                    mPresenter.getEarningsList(nowPage, serviceType);
                    vEarningsAdapter.setEnableLoadMore(true);
                }
            }
        });
        emptyView = vHeaderView.findViewById(R.id.emptyView);
        vEarningsAdapter.addHeaderView(vHeaderView);
        vRecyclerview.setAdapter(vEarningsAdapter);
        vEarningsAdapter.disableLoadMoreIfNotFullPage(vRecyclerview);
        vEarningsAdapter.setPreLoadNumber(2);

        mPresenter = new EarningsPresenter(this);
        mPresenter.getAgentWallet();
        mPresenter.getSumUserWallet();
    }

    @Override
    protected void setListener() {
        super.setListener();
        commonTitle.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TradeOpenProvider.getInstance().openWithdrawRecordActivity(mContext);
            }
        });
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.getAgentWallet();
                mPresenter.getSumUserWallet();
            }
        });
        vEarningsAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getEarningsList(nowPage, serviceType);
            }
        }, vRecyclerview);
    }

    @Override
    public void showAgentWallet(AgentWalletOutput output) {
        vHeaderView.showAgentWallet(output);
    }

    @Override
    public void showEarningsWallet(JSONObject output) {
        vHeaderView.showAgentWallet(output);
    }

    @Override
    public void showEarningsTypeView(List<EarningsTypeOutput> outputs) {
        if (outputs != null && outputs.size() > 0) {
            serviceType = outputs.get(0).getService_type();
            vHeaderView.showAgentWallet(outputs);
            nowPage = CommonConfig.PAGE_NUM;
            mPresenter.getEarningsList(nowPage, serviceType);
            vEarningsAdapter.setEnableLoadMore(true);
        }
    }

    @Override
    public void showEarningsListView(List<EarningsOutput> outputs, boolean isFirst) {
        if (outputs != null && outputs.size() > 0) {
            if (isFirst) {
                vEarningsAdapter.setNewData(outputs);
            } else {
                vEarningsAdapter.addData(outputs);
            }
            nowPage++;
            emptyView.setVisibility(View.GONE);
        } else if (isFirst) {
            vEarningsAdapter.setNewData(new ArrayList<>());
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                vEarningsAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                vEarningsAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                vEarningsAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(EarningsContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
